import random

from api.contract import ContractAPI
from api.course import CourseAPI
from config import *
from tools import assert_common
from api.login import LoginAPI


# 创建类
class TestAddContractAPI:
    course_id = None
    fileName = None

    # 0 前置处理
    def setup_class(self):
        # 创建实例化对象
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        self.contract_api = ContractAPI()
        # 获取验证码
        response = self.login_api.get_captcha()
        assert_common(response)
        # 登录
        login_data = {
            "username": "admin",
            "password": "HM_2023_test",
            "code": "2",
            "uuid": response.json().get("uuid")
        }
        self.response = self.login_api.login(login_data)
        assert_common(self.response)

    # 1.新增课程成功
    def test01_add_course(self):
        add_course_data = {
            "name": "syCoder1-001-测试开发提升课11",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "测试开发提升课01"
        }
        response = self.course_api.add_course(token=self.response.json().get("token"), add_course_data=add_course_data)
        print(response.json())
        assert_common(response)

    # 2.查询课程列表成功
    def test02_query_course_list(self):
        response = self.course_api.query_course_list(token=self.response.json().get("token"),
                                                     params={"name": "syCoder1-001-测试开发提升课11"})
        print(self.response.json())
        assert_common(response)
        TestAddContractAPI.course_id = response.json().get("rows")[0].get("id")
        print(TestAddContractAPI.course_id)

    # 3.合同上传成功
    def test03_upload_contract(self):
        response = self.contract_api.upload_contract(token=self.response.json().get("token"),
                                                     file_path=f"{project_path}/data/test.pdf")
        print(response.json())
        assert_common(response)
        TestAddContractAPI.fileName = response.json().get("fileName")
        print(TestAddContractAPI.fileName)

    # 4.新建合同成功
    def test04_add_contract(self):
        add_contract_data = {
            "name": "测试888",
            "phone": "13543218888",
            "contractNo": f"HT1001{random.randint(1000, 9999)}",
            "subject": "6",
            "courseId": TestAddContractAPI.course_id,
            "channel": "0",
            "activityId": 77,
            "fileName": TestAddContractAPI.fileName
        }
        response = self.contract_api.add_contract(token=self.response.json().get("token"),
                                                  add_contract_data=add_contract_data)
        print(response.json())
        assert_common(response)

    # 5.查询合同成功
    def test05_query_contract(self):
        response = self.contract_api.query_contract_list(token=self.response.json().get("token"),
                                                         params={"phone": "13543218888"})
        print(response.json())
        assert_common(response)
